/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.controller.comment;

import com.channelsharing.common.cache.ExpireTimeConstant;
import com.channelsharing.common.entity.Paging;
import com.channelsharing.common.utils.DateUtils;
import com.channelsharing.hongqu.portal.api.controller.BaseController;
import com.channelsharing.hongqu.portal.api.entity.OrderComment;
import com.channelsharing.hongqu.portal.api.entity.OrderInfo;
import com.channelsharing.hongqu.portal.api.enums.OrderCommentType;
import com.channelsharing.hongqu.portal.api.service.OrderCommentService;
import com.channelsharing.pub.enums.PayStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 商品信息接口Controller
 *
 * @author liuhangjun
 * @version 2018-06-06
 */
@Api(tags = "商品信息操作接口")
@Validated
@RestController
@RequestMapping("/v1/orderComment")
public class CommentController extends BaseController {

    @Resource
    private OrderCommentService orderCommentService;

    @ApiOperation(value = "获取最近一条评价")
    @GetMapping("/{goodsId}")
    public OrderComment findOne(@PathVariable Long goodsId) {

        OrderComment entity = new OrderComment();
        entity.setGoodsId(goodsId);
        OrderComment orderComment = orderCommentService.findOne(entity);
        if (orderComment == null)
            orderComment = new OrderComment();

        return orderComment;
    }
    @ApiOperation(value = "获取商品评价列表")
    @GetMapping
    public Paging<OrderComment> findPaging(@RequestParam(defaultValue = "0") Integer offset,@RequestParam(defaultValue = "0") Long goodsId,
                                        @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) Integer type) {

        OrderComment entity = new OrderComment();
        entity.setOffset(offset);
        entity.setLimit(limit);
        if(type==OrderCommentType.hasImg.getCode()){
            entity.setType(type);
        }
        entity.setGoodsId(goodsId);

        return orderCommentService.findPaging(entity);

    }

    @ApiOperation(value = "提交一条评价")
    @PostMapping
    public void add(@RequestBody @Validated CommentAddRequestEntity goodsInfoAddRequestEntity) {
        OrderComment entity = new OrderComment();
        BeanUtils.copyProperties(goodsInfoAddRequestEntity, entity);
        entity.setUserId(super.currentUserId());
        if (!"".equals(goodsInfoAddRequestEntity.getImageUrl())) {
            entity.setType(OrderCommentType.hasImg.getCode());
        } else {
            entity.setType(OrderCommentType.noImg.getCode());
        }
        entity.setAvatar(super.currentUser().getAvatar());
        entity.setNickname(super.currentUser().getNickname());
        orderCommentService.add(entity);
    }
}
